"""
Awesome middleware
"""
import re
from django.conf import settings
from django.utils.encoding import smart_str

_END_BODY_RE = re.compile(r'<body([^<]*)>', re.IGNORECASE)
AWESOMENESS = '<div id="awesome" style="position: fixed; bottom: 10px; right: 15px; width: 200px; height: 60px; z-index: 9000;"><a href="http://djangopony.com/" class="ponybadge" border="0" title="Magic! Ponies! Django! Whee!"><img src="http://media.djangopony.com/img/small/badge.png" width="210" height="65" alt="ponybadge"></a></div>'

class AwesomeMiddleware(object):
    """
    Middleware that makes your django application awesome.

    """

    def __init__(self):
        #is there even a question?
        self.is_awesome = True
         
        #try referencing some awesomeness from your settings file, yes you can customize the awesome from your setting.py file
        try:
          self.awesomeness = settings.AWESOMENESS
        except:
  	    self.awesomeness = AWESOMENESS
  		
    def make_awesome(self):
        """
  	    Do we really need to document this? It's obviously responsible for making your application awesome.
  	
  	    Note: Override to make your application even more awesome, if that's even possible.
  	
        """
        return self.awesomeness
  
    def __process_awesome_response(self, request, response):
        """
        Handles rendering the awesome. Much more awesome than the boring "process_response" method,
        so we obviously had to provide something a little more awesome.

        Private access because not everyone method can be this awesome.

        """
        response.content = _END_BODY_RE.sub(smart_str('<body\\1>' + self.make_awesome()), response.content)
        return response
  
    def process_response(self, request, response):
        """
        The out-of-the-box "process_response" method isn't awesome enough, 
        so we hand it off to the private _process_awesome_response method which 
        is obviously much more awesome than "process_response"
  
        """
        return self.__process_awesome_response(request, response)



